package solution1.demo.demo15;



import java.util.Arrays;
   class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
   class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }

public class Solution {

    //1.快速排序
    public void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private void quick(int[] arr, int left, int right) {
        if(left >= right) {
            return;
        }
        //降低递归层次
        if(right - left <= 7) {
            insertSort(arr, left, right);
        }
        //三数取中
        int mid = fundMid(arr, left, right);
        swap(arr, left, mid);
        //寻找基准
        int point = fundPoint(arr, left, right);
        quick(arr, left, point - 1);
        quick(arr, point + 1, right);
    }

    private void insertSort(int[] arr, int left, int right) {
        for(int i = left + 1; i <= right; i++) {
            int key = arr[i];
            int j = i - 1;
            for(; j >= left; j--) {
                if(arr[j] > key) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = key;
        }
    }

    private void swap(int[] arr, int left, int mid) {
        int tmp = arr[left];
        arr[left] = arr[mid];
        arr[mid] = tmp;
    }

    private int fundMid(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if(arr[left] < arr[right]) {
            if(arr[mid] > arr[right]) {
                return right;
            } else if(arr[mid] < arr[left]) {
                return left;
            } else {
                return mid;
            }
        } else {
            if(arr[left] < arr[mid]) {
                return left;
            } else if(arr[right] > arr[mid]) {
                return right;
            } else {
                return mid;
            }
        }
    }

    private int fundPoint(int[] arr, int start, int end) {
        int key = arr[start];
        while(start < end) {
            while(start < end && arr[end] >= key) {
                end--;
            }
            arr[start] = arr[end];
            while(start < end && arr[start] <= key) {
                start++;
            }
            arr[end] = arr[start];
        }
        arr[start] = key;
        return start;
    }

    //2.两两相交的链表
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //先分别计算两条路的长短
        ListNode cur1 = headA;
        ListNode cur2 = headB;
        int len1 = 0;
        int len2 = 0;
        while(cur1 != null) {
            cur1 = cur1.next;
            len1++;
        }
        while(cur2 != null) {
            cur2 = cur2.next;
            len2++;
        }
        cur1 = headA;
        cur2 = headB;
        //长的一方先走多出来那几步
        if(len1 > len2) {
            int ans = len1 - len2;
            while(cur1 != null && ans != 0) {
                cur1 = cur1.next;
                ans--;
            }
            //再让两个同时走
            while(cur1 != null && cur2 != null && cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            if(cur1 == cur2) {
                return cur1;
            } else {
                return null;
            }
        } else {
            int ans = len2 - len1;
            while(cur2 != null && ans != 0) {
                cur2 = cur2.next;
                ans--;
            }
            //再让两个同时走
            while(cur1 != null && cur2 != null && cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            if(cur1 == cur2) {
                return cur1;
            } else {
                return null;
            }
        }

    }

    //3.平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        if(Math.abs(maxPath(root.left) - maxPath(root.right)) > 1) {
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }

    private int maxPath(TreeNode root) {
        if(root == null)  {
            return 0;
        }
        return Math.max(maxPath(root.left) + 1, maxPath(root.right) + 1);
    }

    //4.二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(root == p || root == q) {
            return root;
        }
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null && right == null) {
            return null;
        } else if(left == null && right != null) {
            return right;
        } else if(left != null && right == null) {
            return left;
        } else {
            return root;
        }
    }


}

class Main {
    public static void main(String[] args) {
        int[] arr = {3,1,2,5,6,4,3};
        Solution solution = new Solution();
        solution.quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }

}

